//
//  SLRepaymentTableViewCell.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/22.
//

#import "SLRepaymentTableViewCell.h"
#import <YYKit.h>
#import <Masonry.h>
#import "SLInformationOptionComponent.h"
#import <TYTabPagerBar.h>
#import "OriginData.h"


@interface SLRepaymentTabbarCell : UICollectionViewCell<TYTabPagerBarCellProtocol>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign) BOOL isSelected;

+ (NSString *)cellIdentifier;
@end
@implementation SLRepaymentTabbarCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.titleLabel];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}
- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (isSelected) {
        _bgView.backgroundColor = UIColorHex(#FDC5E0);
    } else {
        _bgView.backgroundColor = UIColorHex(#FFFFFF);
    }
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _bgView.layer.borderWidth = 1.5;
        _bgView.layer.cornerRadius = 20;
    }
    return _bgView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = [UIColor darkTextColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
+ (NSString *)cellIdentifier {
    return @"SLRepaymentTabbarCell";
}

@end


@interface SLRepaymentTableViewCell ()<TYTabPagerBarDelegate, TYTabPagerBarDataSource>
@property (nonatomic, strong) UIView *bgView;// 容器
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) TYTabPagerBar *tabBar;
@property (nonatomic, strong) UIStackView *stackView;// 容器
@end
@implementation SLRepaymentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _titles = @[@"E-Wallet", @"Bank"];
        _selectIndex = 0;
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColorHex(#F5F5F5);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.decorate];
        
        [self.bgView addSubview:self.tabBar];
        [self.bgView addSubview:self.stackView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView).inset(0);
            make.top.equalTo(self.contentView).offset(25*0.5);
            make.bottom.equalTo(self.contentView);
        }];
        [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.centerY.equalTo(self.bgView.mas_top);
            make.height.mas_equalTo(25);
        }];
        [self.tabBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(10);
            make.height.mas_equalTo(45);
            make.top.equalTo(self.bgView).offset(34);
        }];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tabBar.mas_bottom).offset(19);
            make.left.right.equalTo(self.bgView).inset(35);
            make.bottom.equalTo(self.bgView).inset(155);
        }];
        
        [self.tabBar reloadData];
    }
    return self;
}

- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    [self.tabBar reloadData];
}


#pragma mark - TYTabPagerBarDataSource
- (NSInteger)numberOfItemsInPagerTabBar {
    return self.titles.count;
}

- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    return aspectValue(135);
}
- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    SLRepaymentTabbarCell *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[SLRepaymentTabbarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = self.titles[index];
    cell.isSelected = self.selectIndex == index;
    return cell;
}

#pragma mark - TYTabPagerBarDelegate
- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    !self.tabbarClickCallback ?: self.tabbarClickCallback(index);
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    [self.tabBar reloadData];
}

- (void)setItems:(NSArray<NSDictionary *> *)items {
    _items = items;
    [self.stackView removeAllSubviews];
    
    NSDictionary *obj = items[_selectIndex];
    for (NSDictionary *dict in obj[@"guest"]) {
        SLInformationOptionComponent *item = [self optionItem];
        item.titleLabel.text = @"E-Wallet";
        item.textField.placeholder = @"Please Select E-Wallet";
        [self.stackView addArrangedSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(65);
        }];
        
        // 赋值
        NSString *cheerfully = dict[@"cheerfully"]; //表单类型
        NSString *formType = [OriginData cheerfullyOrigin:cheerfully];
        item.titleLabel.text = dict[@"conception"] ?: @"Name";
        item.textField.placeholder = dict[@"thoroughly"];
        item.textField.text = dict[@"lake"];
        item.textField.keyboardType = [dict[@"mast"] sl_intValue] == 1 ? UIKeyboardTypeNumberPad : UIKeyboardTypeDefault;
        
        if (![formType isEqualToString:@"txt"]) {
            @weakify(self);
            item.textField.enabled = NO;
            [item jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                @strongify(self);
                !self.itemClickCallback ?: self.itemClickCallback([obj[@"guest"] indexOfObject:dict]);
            }];
        } else {
            [item.textField addBlockForControlEvents:UIControlEventEditingChanged block:^(UITextField *sender) {
                [dict setValue:sender.text forKey:@"lake"];
                [dict setValue:sender.text forKey:@"peppers"];
            }];
        }
        item.more.hidden = [formType isEqualToString:@"txt"];
    }
}


- (SLInformationOptionComponent *)optionItem {
    SLInformationOptionComponent *item = [SLInformationOptionComponent new];
    return item;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.layer.cornerRadius = 20;
        _bgView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _bgView.layer.borderWidth = 1.5;
    }
    return _bgView;
}
- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
        _decorate.contentMode = UIViewContentModeScaleAspectFit;
        _decorate.image = [UIImage imageNamed:@"icon_circle_bar"];
    }
    return _decorate;
}
- (TYTabPagerBar *)tabBar {
    if (!_tabBar) {
        _tabBar = [TYTabPagerBar new];
        _tabBar.delegate = self;
        _tabBar.dataSource = self;
        _tabBar.layout.adjustContentCellsCenter = YES;
        _tabBar.layout.barStyle = TYPagerBarStyleNoneView;
        _tabBar.layout.cellSpacing = 21;
        _tabBar.layout.selectedTextFont = aspectDefaultFont(16);
        _tabBar.layout.normalTextFont = aspectDefaultFont(16);
        _tabBar.layout.selectedTextColor = UIColorHex(#1B1B1B);
        _tabBar.layout.normalTextColor = UIColorHex(#1B1B1B);
        [_tabBar registerClass:SLRepaymentTabbarCell.class forCellWithReuseIdentifier:[SLRepaymentTabbarCell cellIdentifier]];
    }
    return _tabBar;
}
- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.spacing = 19;
        _stackView.axis = UILayoutConstraintAxisVertical;
    }
    return _stackView;
}

@end
